//
//  PlayerControlView.swift
//  Personal
//
//  Created by Ans on 2017/8/3.
//  Copyright © 2017年 Mengxuan Chen. All rights reserved.
//

import UIKit

class PlayerControlView: UIView {
    
    @IBOutlet weak var playBtn: UIButton!
    @IBOutlet weak var fullBtn: UIButton!
    @IBOutlet weak var timeLabel: UILabel!
    
    @IBOutlet weak var sliderView: UISlider!
    @IBOutlet weak var fullBtnTrailling: NSLayoutConstraint!
    
    var progress: Float = 0 {
        didSet {
            self.sliderView.value = self.progress
        }
    }
    

    // block
    var playBtnCallback: ((Bool)->())?
    var fullBtnCallback: ((Bool)->())?
    var sliderValueChangedCallback:((Float)->())?
    var sliderTouchCancelCallback:((Float)->())?
    var sliderTouchBeganCallback:((Float)->())?
    override func awakeFromNib() {

        autoresizingMask = [.flexibleWidth,.flexibleTopMargin]
        
        // 屏蔽父视图响应
        let tap = UITapGestureRecognizer(target: self, action: #selector(singleTap))
        addGestureRecognizer(tap)
        
        //添加滑块响应事件
        sliderView.addTarget(self, action: #selector(PlayerControlView.valueChange(_:)), for: .valueChanged)
        sliderView.addTarget(self, action: #selector(PlayerControlView.sliderTouchCancel(_:)), for: [.touchUpInside, .touchUpOutside, .touchCancel])
        sliderView.addTarget(self, action: #selector(PlayerControlView.sliderTouchBegan(_:)), for: .touchDown)
        
        
        if UIScreen.main.bounds.size.height == 812 {
            fullBtn.isHidden = true
        }
    }
    
    @objc func valueChange(_ sender:UISlider){
    
        print("》》》》》》》滑动改变")
        self.sliderValueChangedCallback!(sender.value)
    }
    
    @objc func sliderTouchCancel(_ sender:UISlider) {
         print("》》》》》》》》滑动取消")
        sliderTouchCancelCallback!(sender.value)
    }
    
    @objc func sliderTouchBegan(_ sender:UISlider) {
        print("》》》》》》》》滑动开始")
        sliderTouchBeganCallback!(sender.value)
        
    }
    

    @IBAction func playBtnClick(_ sender: UIButton) {
        sender.isSelected = !sender.isSelected
        playBtnCallback?(sender.isSelected)
    }
    
    @IBAction func fullBtnClick(_ sender: UIButton) {
        sender.isSelected = !sender.isSelected
        fullBtnCallback?(sender.isSelected)
    }
    
    @objc func singleTap() { print("点击工具栏") }
}

extension PlayerControlView {
    static func loadNib() -> PlayerControlView {
    
        return Bundle.main.loadNibNamed("PlayerControlView", owner: nil, options: nil)?.first as! PlayerControlView
    }
}
